package cnn.user.controller;

import cnn.topfox.annotation.TokenOff;
import cnn.topfox.common.Response;
import cnn.topfox.misc.Misc;
import cnn.user.service.DeptService;
import cnn.user.entity.DeptDTO;
import cnn.user.entity.DeptQTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门基础表
 */
@RestController
@RequestMapping("/api/dept")
public class DeptController {
    @Autowired
    DeptService deptService;

    @PostMapping
    public Response<DeptDTO> insert(@RequestBody DeptDTO dto) {
        deptService.insert(dto);
        return new Response<>(dto, 1);
    }

    @PutMapping
    public Response<DeptDTO> update(@RequestBody DeptDTO dto) {
        Misc.checkObjNotNull(dto, "id");
        int count = deptService.update(dto);
        return new Response<>(dto, count);
    }

    @DeleteMapping("/{ids}")
    public Response<Integer> delete(@PathVariable("ids") Long ids) {
        Misc.checkObjNotNull(ids, "ids");
        int count = deptService.deleteByIds(ids);
        return new Response<>(count);
    }

    @TokenOff
    @GetMapping
    public Response<List<DeptDTO>> list(DeptQTO qto) {
        return deptService.list(qto);
    }

    @TokenOff
    @GetMapping("/{id}")
    public Response<DeptDTO> get(@PathVariable("id") Long id) {
        Misc.checkObjNotNull(id, "id");

        DeptDTO detp = deptService.getObject(id);
        return new Response(detp);
    }

}